package us.pigsoft.client;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.awt.image.RGBImageFilter;
import java.awt.image.RescaleOp;

public class MainInterface extends Applet implements MouseListener, 
													MouseMotionListener,
													KeyListener {
	
	// Static pointer to self (for teh lulz)
	static MainInterface me;
	
	//size
	Dimension size;
	
	//double buffering
	Image buffer;
	Graphics bufferGraphics;
	
	//game
	Game game;
	
	//current interface
	GuiInterface gui;
	
	public MainInterface()
	{
		// Spread the rainbow love forever and evarrr!!1
		MainInterface.me = this;
	}
	
	@Override
	public void init() {
		super.init();
		
		//set window size
		size = new Dimension(640, 580);
		setSize(size);
		resize(size);
		
		//set background
		setBackground(Color.black);
		
		//set up double buffering
		buffer = createImage(size.width, size.height);
		bufferGraphics = buffer.getGraphics();
		
		//start game loop
		game = new Game(this);
		game.start();
		
		// Register event listeners
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		this.addKeyListener(this);
	}
	
	@Override
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D)g;
		
		//enable AA
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		//OCR A Extended
		Font textFont = new Font("Courier New", Font.PLAIN, 18);
		g2.setFont(textFont);
		
		//draw elements
		if(game.running)
			gui.drawScreenElements(g2);
	}
	
	@Override
	public void update(Graphics g) {
		//clear screen
		bufferGraphics.setColor(getBackground());
		bufferGraphics.fillRect(0, 0, size.width, size.height);
        bufferGraphics.setColor(Color.black);
        buffer.flush();
        
        //draw
        paint(bufferGraphics);
        
        //swap buffers
        g.drawImage(buffer, 0, 0, this);
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// Check to see if the mouse clicks any element
		
		for(int i = 0; i < gui.elements.size(); i++)
		{
			if(getMousePosition() != null && gui.elements.get(i).bounds.contains(getMousePosition()))
			{
				// Mouse has clicked element
				gui.elements.get(i).click();
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent mouse) {
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		if(arg0.getButton() == arg0.BUTTON1)
			InputHandler.setMousePress(true);
		else
			InputHandler.setMousePress(false);
		
		for(int i = 0; i < gui.elements.size(); i++)
		{
			if(getMousePosition() != null && gui.elements.get(i).bounds.contains(getMousePosition()))
			{
				// Mouse has clicked element
				gui.elements.get(i).pressed(true);
				System.out.println(gui.elements.get(i).toString());
				System.out.println(getMousePosition().toString());
			}
		}
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		InputHandler.setReleased();
		for(int i = 0; i < gui.elements.size(); i++)
		{
			if(getMousePosition() != null && gui.elements.get(i).bounds.contains(getMousePosition()))
			{
				// Mouse has clicked element
				gui.elements.get(i).pressed(false);
				System.out.println(gui.elements.get(i).toString());
				System.out.println(getMousePosition().toString());
			}
		}
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// Check to see if the mouse is in any element
		for(GuiElement g : gui.elements)
		{
			if(getMousePosition() != null && g.bounds.contains(getMousePosition()))
			{
				g.entered(true);
			} else {
				g.entered(false);
			}
		}
	}

	public void fade(Game.State next) {
		//make an argb buffer
		BufferedImage b =  new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
		b.getGraphics().drawImage(buffer, 0, 0, null);
		
		//get graphics to screen
		Graphics2D g2 = (Graphics2D)getGraphics();
		
		
		float[] scales = { 1f, 1f, 1f,};
		float[] offsets = new float[4];
		RescaleOp rop = new RescaleOp(scales, offsets, null);


		for(int i = 0; i < 20; i++) {
			
			double timeStep = System.currentTimeMillis();
			
			scales[0] -= 0.05;
			scales[1] -= 0.05;
			scales[2] -= 0.05;
			rop = new RescaleOp(scales, offsets, null);
			
			g2.drawImage(b, rop, 0, 0);
			
			//wait for next frame
			try {
				while(System.currentTimeMillis()-timeStep < 1000/60.0) {
					Thread.sleep(0);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		try {
			Thread.sleep(100);
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}

		//change to next screen
		game.currentState = next;
		b = (BufferedImage) createImage(b.getWidth(), b.getHeight());
		game.interfaces[next.ordinal()].drawScreenElements((Graphics2D)b.getGraphics());
		
		scales[0] = scales[1] = scales[2] = 0.0f;
		
		//then fade in
		for(int i = 0; i < 20; i++) {
			double timeStep = System.currentTimeMillis();
			
			scales[0] += 0.05;
			scales[1] += 0.05;
			scales[2] += 0.05;
			rop = new RescaleOp(scales, offsets, null);
			
			g2.drawImage(b, rop, 0, 0);
			
			//wait for next frame
			try {
				while(System.currentTimeMillis()-timeStep < 1000/60.0) {
					Thread.sleep(0);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		char key = e.getKeyChar();
		InputHandler.setKey((int)key, true);
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		char key = e.getKeyChar();
		InputHandler.setKey((int)key, false);
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
}
